package com.baidu.springcloud.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ShiLei
 * @version v1.0
 * @date 2020-03-19 20:22
 */
@Configuration
public class GateWayConfig {

    /**
     * 配置了一个id为path_baidu_id 的路由规则
     * 当访问地址http://localhost:9527/game时会自动转发到地址 http://news.baidu.com/game
     *
     * @param routeLocatorBuilder
     * @return
     */
    @Bean
    public RouteLocator getRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
        routes.route("path_baidu_id", r -> r
                .path("/game")
                .uri("http://news.baidu.com/game"))
                .build();
        return routes.build();
    }
    @Bean
    public RouteLocator getRouteLocator2(RouteLocatorBuilder routeLocatorBuilder) {
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
        routes.route("path_baidu_id2", r -> r
                .path("/guonei")
                .uri("http://news.baidu.com/guonei"))
                .build();
        return routes.build();
    }
}
